package com.example.ware.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.ware.dao.WareMapper;
import com.example.ware.model.Spage.ShowPage;
import com.example.ware.model.po.Place;
import com.example.ware.model.po.Ware;
import com.example.ware.model.vo.WareInfo;
import com.example.ware.service.PlaceService;
import com.example.ware.service.WareService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class WareServiceImpl extends ServiceImpl<WareMapper, Ware> implements WareService {

    @Resource
    WareMapper wareMapper;


    @Autowired
    PlaceService placeService;

    @Override
    public ShowPage getWareList(String name, String location, Integer pageNum, Integer pageSize) {



        ShowPage showPage = new ShowPage();
        Integer pageBegin = (pageNum-1) * pageSize;
        showPage.setPageNum(pageNum);
        //总数据条数
        List<Ware> all=wareMapper.getWareAll(name,location);
        showPage.setSize(all.size());
        //每页数据条数
        showPage.setPageSize(pageSize);
        //总页数
        Integer div = showPage.getSize()/pageSize;
        Integer pages = showPage.getSize() % pageSize == 0 ? div : div + 1;
        showPage.setPages(pages);


        List<WareInfo> datas=wareMapper.getWareList(name,location,pageBegin,pageSize);

        List<WareInfo> result=new ArrayList<>();

        for (WareInfo w:datas){
            QueryWrapper<Place> qw=new QueryWrapper<>();
            qw.eq("wid",w.getId());
            List<Place> list = placeService.list(qw);
            w.setPlaceList(list);
            result.add(w);
        }

        showPage.setData(result);

        return showPage;

    }
}
